<template>
  <a-card class="archive_div">
    <a-tabs v-model="activityKey" @change="change">
      <a-tab-pane key="1" tab="归档信息" force-render>
        <ArchiveInfoAdd
          v-if="serviceType == St_serviceTypeValue.REVIEW"
          ref="archiveInfoAddRef"
          :applyid="applyid"
          :isEdit="isEdit"
          :reason="reason"
        ></ArchiveInfoAdd>
        <ArchiveSuperviseInfoAdd
          v-if="serviceType == St_serviceTypeValue.SUPERVISE"
          ref="archiveSuperviseInfoAddRef"
          :applyid="applyid"
          :isEdit="isEdit"
          :reason="reason"
        ></ArchiveSuperviseInfoAdd>
      </a-tab-pane>
      <a-tab-pane key="2" tab="电子卷宗" force-render>
        <ArchiveDoc
          ref="archiveDocRef"
          :applyid="applyid"
          :isEdit="isDocEditVal"
          :serviceType="serviceType"
        ></ArchiveDoc>
      </a-tab-pane>
    </a-tabs>
    <div class="buttonBox" v-show="isEdit">
      <a-button type="primary" v-show="activityKey == '1'" class="button-style" @click="save" :loading="loading">
        保存
      </a-button>
      <a-button type="primary" v-show="activityKey == '2'" class="button-style" @click="record" :loading="loading">
        归档
      </a-button>
    </div>
  </a-card>
</template>

<script>
import { queryLastTaskidByInstid, getAdmirePersonInfo, recordCase } from '@/api/archive/apl_archive'
import { St_admreview } from '@/enumeration/St_admreview'
import { St_admreviewsonValue } from '@/enumeration/St_admreviewson.js'
import storage from 'store'
import { APP_USER } from '@/store/mutation-types.js'
import { updateTreeNodeIndex } from '@/api/archive/apl_archive.js'
import { St_serviceTypeValue } from '@/enumeration/St_serviceType.js'
export default {
  props: {
    applyid: {
      type: String,
      default: '',
      required: true
    },
    instid: {
      type: String,
      default: '',
      required: true
    },
    taskid: {
      type: String,
      default: '',
      required: false
    },
    processinstanceid: {
      type: String,
      default: '',
      required: false
    },
    isEditVal: {
      type: Boolean,
      default: true,
      required: true
    },
    isDocEditVal: {
      type: Boolean,
      default: true,
      required: true
    },
    reason: {
      type: String,
      default: '',
      required: false
    },
    serviceType: {
      type: Number,
      required: true
    }
  },

  data () {
    return {
      // applyid: '',
      // instid: '',
      activityKey: '1',
      isEdit: undefined,
      isDocEdit: undefined, // 文本是否可以编辑
      // taskid: '',
      armireInfo: {},
      performBtn: false,
      punishBtn: false,
      urgeBtn: false,
      forceBtn: false,
      St_admreviewson: St_admreviewsonValue,
      St_admreview: St_admreview,
      currentUser: storage.get(APP_USER), // 登录人信息
      St_serviceTypeValue: St_serviceTypeValue,
      loading: false
    }
  },
  components: {
    ArchiveInfoAdd: () => import('@/views/archive/ArchiveInfoAdd.vue'),
    ArchiveSuperviseInfoAdd: () => import('@/views/archive/ArchiveSuperviseInfoAdd.vue'),
    ArchiveDoc: () => import('@/views/archive/ArchiveDoc.vue')
  },
  created () {
    this.init()
  },
  mounted () {},
  methods: {
    init () {
      // this.applyid = this.$route.query.params.applyid
      // this.instid = this.$route.query.params.instid
      this.isEdit = this.isEditVal
      this.isDocEdit = this.isDocEditVal
      if (
        this.instid != null &&
        this.instid != '' &&
        this.instid != undefined &&
        this.taskid != null &&
        this.taskid != '' &&
        this.taskid != undefined
      ) {
        this.getTaskId()
      } else {
        this.$message.error('流程实例id为空')
      }
      if (this.applyid != null && this.applyid != '' && this.applyid != undefined) {
        if (this.serviceType == St_serviceTypeValue.REVIEW) {
          this.getAdmirePersonInfo()
        }
      } else {
        this.$message.error('当前applyid为空')
      }
    },
    initComponent (component) {
      this.$refs[component].init()
    },
    /**
     * 获取流程任务id
     */
    getTaskId () {
      queryLastTaskidByInstid({ processinstanceid: this.instid }).then(res => {
        if (res.code !== 0) {
          this.$message.error('获取流程实例错误')
        } else {
          this.taskid = res.data
        }
      })
    },
    /**
     * 获取当前的状态及申请人第三人信息
     * @param {} key
     */
    getAdmirePersonInfo () {
      const _this = this
      getAdmirePersonInfo({ applyid: this.applyid }).then(res => {
        if (res.code !== 0) {
          this.$message.error('获取申请人第三人信息错误')
        } else {
          this.armireInfo = res.data
          const admreviewInfo = this.armireInfo.apl_admreview
          const sonstatus = admreviewInfo.sonstatus
          // 判断当前的状态是否是归档
          if (admreviewInfo.status && admreviewInfo.status == _this.St_admreview.archive.value) {
            this.isEdit = false
          }
          // isforce 是否强制执行
          // isurging 是否履行催告
          // isperform 是否责令履行
          // ispunish  是否政务处分
          // 获取流程的子状态：
          // 当前的状态是责令履行
          if (sonstatus && sonstatus.indexOf(this.St_admreviewson.PERFORM) > -1) {
            this.performBtn = false
          } else {
            this.performBtn = !admreviewInfo.isperform
          }
          // 判断当前是否是政务处分
          if (this.performBtn || (sonstatus && sonstatus.indexOf(this.St_admreviewson.PUNISH) > -1)) {
            this.punishBtn = false
          } else {
            this.punishBtn = !admreviewInfo.ispunish
          }

          // 当前的状态是履行催告
          if (sonstatus && sonstatus.indexOf(this.St_admreviewson.URGING) > -1) {
            this.urgeBtn = false
          } else {
            this.urgeBtn = !admreviewInfo.isurging
          }
          // 判断当前是否是强制执行
          if (this.urgeBtn || (sonstatus && sonstatus.indexOf(this.St_admreviewson.FORCE) > -1)) {
            this.forceBtn = false
          } else {
            this.forceBtn = !admreviewInfo.isforce
          }
        }
      })
    },
    async change (key) {
      if (key == '1') {
        // 触发文件树的更新
        const res = await updateTreeNodeIndex({ applyid: this.applyid })
        if (this.serviceType == St_serviceTypeValue.REVIEW) {
          if (this.$refs.archiveInfoAddRef) {
            this.$refs.archiveInfoAddRef.getArchiveData()
          }
        } else if (this.serviceType == St_serviceTypeValue.SUPERVISE) {
          if (this.$refs.archiveSuperviseInfoAddRef) {
            this.$refs.archiveSuperviseInfoAddRef.getArchiveData()
          }
        }
      }
      this.activityKey = key
    },
    reload () {
      this.getAdmirePersonInfo()
    },
    save () {
      if (this.serviceType == St_serviceTypeValue.REVIEW) {
        this.loading = true
        try {
          this.$refs.archiveInfoAddRef.save()
        } finally {
          this.loading = false
        }
      } else if (this.serviceType == St_serviceTypeValue.SUPERVISE) {
        this.loading = true
        try {
          this.$refs.archiveSuperviseInfoAddRef.save()
        } finally {
          this.loading = false
        }
      }
    },
    record () {
      this.$confirm({
        title: '提示',
        content: '是否确认完成归档？完成归档后不能再次编辑！',
        onOk: () => {
          if (this.serviceType == St_serviceTypeValue.REVIEW) {
            if (this.$refs.archiveInfoAddRef.isValidate()) {
              this.loading = true
              recordCase({
                applyid: this.applyid,
                userId: this.currentUser.userId,
                userName: this.currentUser.fullname,
                serviceType: this.serviceType
              })
                .then(res => {
                  if (res.code !== 0) {
                    this.$message.error('案件归档失败')
                  } else {
                    this.$message.success('归档成功')
                    this.$router.push({
                      path: '/ArchiveUndoList',
                      query: {
                        url: '/ArchiveUndoList'
                      }
                    })
                  }
                })
                .finally(() => {
                  this.loading = false
                })
            } else {
              this.activityKey = '1'
              this.$message.warn('归档信息未填完成')
            }
          } else {
            if (this.$refs.archiveSuperviseInfoAddRef.isValidate()) {
              this.loading = true
              recordCase({
                applyid: this.applyid,
                userId: this.currentUser.userId,
                userName: this.currentUser.fullname,
                serviceType: this.serviceType
              })
                .then(res => {
                  if (res.code !== 0) {
                    this.$message.error('案件归档失败')
                  } else {
                    this.$message.success('归档成功')
                    this.$router.push({
                      path: '/ArchiveUndoList',
                      query: {
                        url: '/ArchiveUndoList'
                      }
                    })
                  }
                })
                .finally(() => {
                  this.loading = false
                })
            } else {
              this.activityKey = '1'
              this.$message.warn('归档信息未填完成')
            }
          }
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.buttonBox {
  width: 100%;
  background-color: #fff;
  position: fixed;
  bottom: 0px;
  right: 0;
  height: 60px;
  box-shadow: 1px 1px 10px rgba(49, 45, 45, 0.377);
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding-right: 5%;
  z-index: 9;
}

.button-style {
  margin-right: 10px;
}
.archive_div {
  ::v-deep .ant-card-body {
    padding: 10px 16px !important;
  }
  ::v-deep .ant-tabs-nav-wrap {
    height: 50px;
  }
  ::v-deep .ant-tabs-ink-bar {
    margin: 0px 8% !important;
    width: 50px !important;
  }
  ::v-deep .ant-tabs-nav-scroll {
    background: white;
    margin-left: 0px !important;
  }
  // ::v-deep .ant-tabs-nav-wrap {
  //   height: 65px !important;
  //   margin-top: 30px !important;
  // }
  // ::v-deep .ant-breadcrumb {
  //   margin-left: 15px !important;
  // }
}
</style>
